<template>
	<view class="home">
		<uni-search-bar @confirm="searchGoods" bgColor="#fff" v-model="searchKey" />
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,index) in bannerGoodsList" :key="index">
				<view class="swiper-item">
					<image :src="item" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 商品分类 -->
		<view class="cate">
			<view class="cate-item" v-for="(item,index) in cateOneLst" :key="index">
				<image class="cate-img" :src="item.icon" mode="widthFix"></image>
				<view class="cate-name">{{item.name}}</view>
			</view>
		</view>

		<!-- 精选商品列表 -->
		<view class="show-goods">
			<view class="title">
				精选商品 <image class="icon" :src="showImg" @tap="changShow"></image>
			</view>
			<view v-if="showDouble">
				<GoodsList :selectedLst="selectedLst"></GoodsList>
			</view>
			<view v-else>
				<GoodsList2 :selectedLst="selectedLst"></GoodsList2>
			</view>
			
		</view>
	</view>
</template>

<script setup>
	import GoodsList from "../../components/GoodsList.vue"
	import GoodsList2 from "../../components/GoodsList2.vue"
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	// 搜索功能
	const searchKey = ref("")

	function searchGoods() {
		console.log(searchKey.value)
	}



	onLoad(() => {
		getBanner()
		getCateOneLst()
		getSelectedList()
		 
	})


	// 轮播图
	const bannerGoodsList = ref([])
	// 获取轮播图图片
	function getBanner() {
		uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 5
		}).then((res) => {
			// console.log(res)
		 
				// 将商品列表中的图片信息提取
				bannerGoodsList.value = res.goodsList.map(item => uni.$base_url + item.goods_big_logo)
				// 控制台显示测试接口数据
				// console.log(bannerGoodsList.value)
			 
			// console.log(bannerGoodsList)
		}) 
	}
	//一级分类列表
	const cateOneLst = ref([])

	function getCateOneLst() {
		uni.$post("/categoryListLevel", {
			"level": 0
		}).then((res => {


			cateOneLst.value = res.result.map(item => {
				item.icon = uni.$base_url + item.icon
				return item
			})
			// console.log(cateOneLst.value)

		}))
	}


	// 精选商品列表
	const selectedLst = ref([])

	function getSelectedList() {
		uni.$post("/goodsPromoteSelect", {
			"is_selected": 1,
			"is_promote": 0,
			"page": 1,
			"number": 30
		}).then(res => {
			// console.log(res)

			selectedLst.value = res.goodsList.map(item => {
				item.goods_big_logo = uni.$base_url + item.goods_big_logo
				return item;
			})
		})

	}

     const showDouble=ref(true)
	 const showImg=ref("/static/images/double.png")
	 function changShow(){
		 showDouble.value=!showDouble.value
		 showImg.value=showDouble.value?"/static/images/double.png":"/static/images/single.png"
		 
	 }
	 

	const toDetails = (id) => {
		uni.navigateTo({
			url: "/pages/goodsDetails/goodsDetails?id=" + id
		})
	}
</script>

<style scoped>
	.home {
		background-color: #f7f7f7;
	}

	/* 轮播图中图片宽度 */
	.home .swiper-item>image {
		width: 100%;
	}

	/* 轮播图容器高度 */
	.home swiper {
		height: 200px;
	}

	/* 商品分类容器 */
	.home>.cate {
		display: flex;
		justify-content: space-around;
		background-color: #fff;
		margin: 10rpx 0 10rpx 0;
	}

	/* 每个商品分类 */
	.home>.cate>.cate-item {
		width: 130rpx;
		height: 130rpx;
		/* border:1px solid red; */
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 26rpx;

	}




	/* 商品分类中图片 */
	.home>.cate>.cate-item>.cate-img {
		width: 60rpx;
		height: 60rpx;
		margin-top: 20rpx;
	}
   
	/* 精选商品标题 */
	.show-goods>.title {
		background-color: #fff;
		padding: 15rpx;
		color: #ff6700;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.show-goods> .title .icon{
		width: 20px;
		height: 20px;
	}


</style>